//
//  QWAppContextConfig.m
//  WJAppContext-example
//
//  Created by Yunhai.Wu on 15/12/25.
//  Copyright © 2015年 WJ. All rights reserved.
//

#import "QWAppContextConfig.h"

@interface QWAppContextConfig ()
@property (nonatomic, strong) NSMutableArray *hs;
@end

@implementation QWAppContextConfig

DEF_SINGLETON_INIT(QWAppContextConfig)

-(void) singleInit {
    _hs = [[NSMutableArray alloc] init];
    _tokenClass = NSClassFromString(@"GenericQWToken");
    _keychainDefaultService = [[NSBundle mainBundle] bundleIdentifier];
    _keychainAccessGroup = @"";
}

-(NSArray *)notifyHandles {
    return _hs;
}

-(void) setTokenClass:(Class) wjTokenClass {
    if (wjTokenClass) {
        if ([wjTokenClass conformsToProtocol:NSProtocolFromString(@"IQWToken")]) {
            _tokenClass = wjTokenClass;
        } else {
            NSString *reason = [NSString stringWithFormat:@"%@ 没有实现接口IQWToken",NSStringFromClass(wjTokenClass)];
            @throw [NSException exceptionWithName:@"QWAppContextConfigDomain" reason:reason userInfo:nil];
        }
    } else {
        _tokenClass = nil;
    }
}

-(void) setKeychainAccessGroup:(NSString*) keychainAccessGroup {
    _keychainAccessGroup = [keychainAccessGroup copy];
}

-(void) setKeychainDefaultService:(NSString *) defaultService {
    if (defaultService) {
        _keychainDefaultService = [defaultService copy];
    }
}

-(void) addNotifyHandle:(Class) handleClazz {
    if (handleClazz) {
        if (![_hs containsObject:handleClazz]) {
            if ([handleClazz conformsToProtocol:@protocol(IQWAppNotifyHandler)]) {
                [_hs addObject:handleClazz];
            } else {
                NSString *reason = [NSString stringWithFormat:@"%@ 没有实现接口IQWAppNotifyHandler",NSStringFromClass(handleClazz)];
                @throw [NSException exceptionWithName:@"QWAppContextConfigDomain" reason:reason userInfo:nil];
            }
        }
    }
}

@end
